package com.hframe.utils;

import java.util.regex.Pattern;

/**
 * 对数据格式的判断
 *
 * @author heqian
 */
public class FormatUtils {
    private static final String REGEX_PHONE = "^1[34578]\\d{9}$";
    private static final String REGEX_MOBLIE = "^0\\d{2,3}-?\\d{7,8}$";
    private static final String REGEX_IDCARD = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
    private static final String REGEX_BIRTH = "^[0-9]{4}-[0-1]?[0-9]{1}-[0-3]?[0-9]{1}$";
    private static final String REGEX_SQL = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";
    private static final String REGEX_EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";

    private static Pattern sqlPattern = Pattern.compile(REGEX_SQL, Pattern.CASE_INSENSITIVE);

    /**
     * 判断手机号格式
     * *
     *
     * @param phone 手机号
     * @return 真、假
     */
    public static boolean phone(String phone) {
        return phone != null && (phone.matches(REGEX_PHONE) || phone.matches(REGEX_MOBLIE));
    }


    /**
     * 判断身份证号格式
     *
     * @param idcard 身份证号
     * @return 真、假
     */
    public static boolean idCard(String idcard) {
        return idcard != null && idcard.matches(REGEX_IDCARD);
    }

    /**
     * 判断生日格式
     *
     * @param birth 生日
     * @return 真、假
     */
    public static boolean birth(String birth) {
        return birth != null && birth.matches(REGEX_BIRTH);
    }

    /**
     * 判断是否有SQL关键字
     *
     * @param sql 要过滤的SQL字符串
     * @return 真、假
     */
    public static boolean sql(String sql) {
        return !sqlPattern.matcher(sql).find();
    }

    /**
     * 判断邮箱
     *
     * @param email
     * @return
     */
    public static boolean email(String email) {
        return null != email && !"".equals(email) && email.matches(REGEX_EMAIL);
    }

}
